#!/bin/bash

# 函数：清理 YAML 文件并合并到输出文件
yacm_clear_old() {
    # 逐行读取要删除的字段
    while IFS= read -r field; do
        # 使用 yq 删除特定字段
        yq eval "del(${field})" -i a
    done < del_field.txt
}
yacm_clear() {
    # 使用 yq 删除特定字段
    yq eval 'del(.metadata.managedFields, .metadata.annotations, .metadata.resourceVersion, 
                 .metadata.uid, .metadata.creationTimestamp, .metadata.generation, .spec.volumeName, .spec.volumeMode, .spec.storageClassName,.spec.template.metadata.annotations,
                 .status, .spec.clusterIP, .spec.clusterIPs, .spec.template.metadata.creationTimestamp, .spec.template.spec.nodeSelector.area)' -i a
#    sed  -i "/- name: pinpoint_appname/,+1d"  a
#    sed  -i "/- name: collector_ip/,+1d"  a
#    sed  -i "/- name: pinpoint_id/,+1d" a
#    sed  -i "/- name: pinpoint_ip/,+1d" a
#    sed  -i "/- name: pinpoint_uri/,+1d" a
    sed -i 's/-deployment-a/-deployment/g' a
    sed -i 's/-deployment-b/-deployment/g' a
    sed -i 's/-deployment-c/-deployment/g' a
}

ingress_clear() {
    # 使用 yq 删除特定字段
    yq eval 'del(.metadata.managedFields, .metadata.annotations."kubectl.kubernetes.io/last-applied-configuration", .metadata.resourceVersion, 
                 .metadata.uid, .metadata.creationTimestamp, .metadata.generation, .spec.volumeName, .spec.volumeMode, .spec.storageClassName,.spec.template.metadata.annotations,
                 .status, .spec.clusterIP, .spec.clusterIPs, .spec.template.metadata.creationTimestamp, .spec.template.spec.nodeSelector.area)' -i a
    sed  -i "/- name: pinpoint_appname/,+1d"  a
    sed  -i "/- name: collector_ip/,+1d"  a
    sed  -i "/- name: pinpoint_id/,+1d" a
    sed  -i "/- name: pinpoint_ip/,+1d" a
    sed  -i "/- name: pinpoint_uri/,+1d" a
    sed -i 's/-deployment-a/-deployment/g' a
    sed -i 's/-deployment-b/-deployment/g' a
    sed -i 's/-deployment-c/-deployment/g' a
}

# 函数：解密 secret 的 data 部分
decode_secret_data() {
    # 读取 Secret YAML 文件并解码 data 部分
    yq eval '.data |= with_entries(.value |= @base64d)' -i a
    sed 's/^data:/stringData:/g' -i a
}
#添加ingress参数
add_ingress_class() {
    sed '/kubernetes.io\/ingress.class: nginx/d' -i a
    sed '/ingressClassName: nginx/d' -i a
    # 使用 yq 在 spec 下添加 ingressClassName
    yq eval "(.spec | select(. != null)) += {\"ingressClassName\": \"nginx\"}" -i a
}

# 循环读取命名空间列表
while read -r appid; do
    echo '---' > "$appid.yaml"  # 清空目标 YAML 文件
    echo "Start processing namespace: $appid"

    # 获取命名空间的 YAML 并去掉状态部分
    kubectl get ns "$appid" -o=yaml > a
    yacm_clear  # 调用清理函数
    cat a >> "$appid.yaml"
    echo '---' >> "$appid.yaml"

    # 获取 PVC 并处理
    pvcnames=`kubectl get pvc  -n $appid |grep -v "NAME"   | awk -F " " '{print $1}' |  grep -v "preprod"`
    for pvcname in $pvcnames; do
        if [[ -n $pvcname ]]; then
            kubectl get pvc "$pvcname" -n "$appid" -o=yaml > a
            yacm_clear  # 调用清理函数
            cat a >> "$appid.yaml"
            echo '---' >> "$appid.yaml"
        fi
    done

    # 获取 Secret 并处理，解密 data ��分
    Secretnames=`kubectl get Secret -n $appid |grep -v "NAME" |grep -v "default-token"|grep -v "default-secret"|grep -v "paas.elb"|grep -v "default-token"| awk -F " " '{print $1}'`
    for Secretname in $Secretnames; do
        if [[ -n $Secretname ]]; then
            kubectl get secret "$Secretname" -n "$appid" -o=yaml > a
            # 解密 secret 的 data 部分
            yacm_clear  # 调用清理函数
#            decode_secret_data "$Secretname" "$appid"
            cat a >> "$appid.yaml"
            echo '---' >> "$appid.yaml"
        fi
    done

    # 获取 Configmap 并处理，解密 data 部分
    Configmaps=`kubectl get cm -n $appid |grep -v "NAME" |grep -v "kube-root-ca.crt"|grep -v "istio-ca-root-cert"| awk -F " " '{print $1}'`
    for Configmap in $Configmaps; do
        if [[ -n $Configmap ]]; then
            kubectl get cm "$Configmap" -n "$appid" -o=yaml > a
            # 解密 secret 的 data 部分
            yacm_clear  # 调用清理函数
            cat a >> "$appid.yaml"
            echo '---' >> "$appid.yaml"
        fi
    done


    # 获取 Deployment 并处理
    deploynames=`kubectl get  svc -n $appid -o=wide | grep "\-pre"  -v | grep -v NAME |awk -F " " '{print $7}' | sed "s/run=\(.*\)/\1/g"| sed "s/name=\(.*\)/\1/g"`
    for deployname in $deploynames; do
        if [[ -n $deployname ]]; then
            kubectl get deploy "$deployname" -n "$appid" -o=yaml > a
            yacm_clear  # 调用清理函数
            yq e '.spec.replicas = 1' -i a
            cat a >> "$appid.yaml"
            echo '---' >> "$appid.yaml"
        fi
    done

    # 获取 Service 并处理
    svcnames=`kubectl get  svc -n $appid  -o=wide | grep "\-pre"  -v | grep -v NAME |awk -F " " '{print $1}'`
    for svcname in $svcnames; do
        if [[ -n $svcname ]]; then
            kubectl get service "$svcname" -n "$appid" -o=yaml > a
            yacm_clear  # 调用清理函数
            cat a >> "$appid.yaml"
            echo '---' >> "$appid.yaml"
        fi
    done

    # 获取 Ingress 并处理
    ingnames=`kubectl get ing -n $appid |grep -v "NAME"  | awk -F " " '{print $1}'`
    for ingname in $ingnames; do
        if [[ -n $ingname ]]; then
            kubectl get ingress "$ingname" -n "$appid" -o=yaml > a
            ingress_clear  # 调用清理函数
            add_ingress_class
            cat a >> "$appid.yaml"
            echo '---' >> "$appid.yaml"
        fi
    done

done < name